package com.practices2.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.GridView;
import android.widget.ImageView.ScaleType;

import com.practices2.activity.ImageListAcitvity;
import com.practices2.base.TAApplication;
import com.practices2.testdata.ImagesUrls;
import com.practices2.util.Util;
import com.practices2.util.bitmap.TABitmapCacheWork;
import com.practices2.util.bitmap.TADownloadBitmapHandler;

public class ImageAdapter extends BaseAdapter {
	
	TABitmapCacheWork imageFetcher;
	Context mContext;
	TAApplication application;
	int imageSize ;

	public ImageAdapter(Context mContext, TAApplication application,int imageSize) {
		super();
		this.mContext = mContext;
		this.application = application;
		this.imageSize=imageSize;
		
		TADownloadBitmapHandler downloadBitmapHandler=new TADownloadBitmapHandler(mContext, imageSize);
		imageFetcher=new TABitmapCacheWork(mContext);
		imageFetcher.setProcessDataHandler(downloadBitmapHandler);
		imageFetcher.setFileCache(application.getFileCache());
		
	}



	@Override
	public int getCount() {
		return ImagesUrls.imageThumbUrls.length;
	}

	@Override
	public Object getItem(int position) {
		return ImagesUrls.imageThumbUrls[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView==null) {
			imageView=new ImageView(mContext);
			
			int imageViewWidth=imageSize-2;
			int imageViewHight=imageViewWidth;
			imageView.setLayoutParams(new GridView.LayoutParams(imageViewWidth,imageViewHight));
			imageView.setScaleType(ScaleType.FIT_XY);
			
		}else {
			imageView=(ImageView)convertView;
		}
		imageFetcher.loadFormCache(ImagesUrls.imageThumbUrls[position], imageView);
		return imageView;
	}

}
